﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.Data.Client.SQLite
{
    public class ClientDbService
    {
        System.Data.SQLiteProvider.SQLiteConfigService config;

        System.Data.SQLiteProvider.SQLiteSqlService sqlService;

        public IList<ClientTable> Table { get; set; }

        public ClientDbService(System.Data.SQLiteProvider.SQLiteConfigService con,
            System.Data.SQLiteProvider.SQLiteSqlService sql)
        {
            if (con == null)
            {
                throw new ArgumentNullException("config is null");
            }
            if (sql == null)
            {
                throw new ArgumentNullException("sqlService is null");
            }

            this.config = con;
            this.sqlService = sql;
            this.sqlService.ConnectionStringBuilder = config.ConnectionStringBuilder;

            this.Table = new List<ClientTable> {
                new ClientTable{ Name = "image" , Sql = "create table image (name text,sightId integer)"},
                new ClientTable{ Name = "line" , Sql = "create table line (SightId integer,Remark text,CustomDateTime text,image text,sortId integer,name text,lat double ,lng double,area integer,day integer,isShow integer)"},
                new ClientTable{ Name = "poi" , Sql = "create table poi (id integer, name text,lng double, lat double, type integer,area integer,image text,remark text,vouchnum integer)"}
            };
        }

        public bool CreateClientDbTable()
        {
            var res = false;
            if (this.Table != null && this.Table.Count > 0)
            {
                List<string> temp = new List<string>();
                foreach (var item in this.Table)
                {
                    temp.Add(item.Sql);
                }
                //在这里先得清空原来的数据，和表，
                sqlService.ExecuteSqlTran(temp);
            }
            return res;
        }

        public bool DropAllClientDbTable()
        {
            var res = false;
            if (this.Table != null && this.Table.Count > 0)
            {
                List<string> temp = new List<string>();
                foreach (var item in this.Table)
                {
                    temp.Add("drop table "+   item.Name);
                }
                //在这里先得清空原来的数据，和表，
                sqlService.ExecuteSqlTran(temp);
            }
            return res;
        }
    }
}
